﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class RegistrationForm : Form
    {
        public RegistrationForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var uid = textBox1.Text;
            var pwd = textBox2.Text;
            var pwd2 = textBox3.Text;
            var uid1 = !string.IsNullOrEmpty(uid);
            var pwd1 = !string.IsNullOrEmpty(pwd);
            var repwd = (pwd == pwd2);

            if (pwd == pwd2)
            {
                var sql = string.Format("select * from Users where Username='{0}'", uid);
                var dt = DbHelper.AccountDatabase(sql);
                SqlDataReader sqlData = dt.ExecuteReader();
                if (!sqlData.Read())
                {
                    sqlData.Close();
                    if (uid1 && pwd1 && repwd)
                    {
                        var sqlStr = string.Format("insert into Users(Username,Password)values('{0}','{1}')", uid, pwd);
                        var dt1 = DbHelper.AccountDatabase(sqlStr);
                        dt1.ExecuteNonQuery();
                        MessageBox.Show("注册成功", "提示");
                        Form1 form1 = new Form1();
                        form1.Show();
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("注册失败", "提示");
                    }


                }
                else
                {
                    MessageBox.Show("你输入的用户名已存在！", "提示");
                }

            }
            else
            {
                MessageBox.Show("二次输入的密码不同！", "提示");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
            this.Close();
        }

        private void RegistrationForm_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }
    }
}
